<?php

class AdminController extends Zend_Controller_Action
{
    public function preDispatch() {

        if (!Zend_Auth::getInstance()->getIdentity()) {
            $this->_redirect('/login/');
        }

        $this->view->headScript()->appendFile($this->view->baseUrl() . '/js/jquery.sortable.js');
        parent::preDispatch();
    }

    public function indexAction(){}

    public function stockAction()
    {
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Stock(), new Application_Model_Stock());
        $this->view->stocks = $mapper->getCollection(array(), 'stock_id DESC');
    }

    public function usefulAction()
    {
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Useful(), new Application_Model_Useful());
        $this->view->usefuls = $mapper->getCollection(array(), 'useful_id DESC');
    }

    public function galleryAction()
    {
        $albumId = (int) $this->_getParam('id');

        if (!$albumId) {
            $this->_redirect('/admin/galleryalbum');
        }

        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Gallery(), new Application_Model_Photo());
        $this->view->photos = $mapper->getCollection(array('album_id' => $albumId), 'photo_id DESC');
    }

    public function galleryalbumAction()
    {
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_GalleryAlbums(), new Application_Model_GalleryAlbums());
        $this->view->albums = $mapper->getCollection();
    }

    public function rentcatAction()
    {
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_RentCategory(), new Application_Model_RentCategory());
        $this->view->categories = $mapper->getCollection(array(), 'category_id DESC');
    }

    public function rentAction()
    {
        $mapper = new Application_Model_Mapper(new Application_Model_DbTable_Rent(), new Application_Model_Rent());
        $category = $mapper->getRentCategoryBy4pu($this->_getParam('category'));

        if (!$category) {
            $this->_redirect('/admin/rentcat');
        }

        $this->view->instruments = $mapper->getRentsByCategory($category);
    }

    public function createAction()
    {
        $form = Application_Model_Factory::createForm($this->_getParam('form'), null, $this->_getAllParams());

        if (!$form) {
            $this->_redirect('/admin/');
        }

        $this->view->form = $form;
        $this->render($form->getTpl());
    }

    public function editAction()
    {
        $id = $this->_getParam('id');
        $form = Application_Model_Factory::createForm($this->_getParam('form'), (int) $id, $this->_getAllParams());

        if (!$form) {
            $this->_redirect('/admin/');
        }

        $this->view->form = $form;
        $this->render($form->getTpl());
    }

    public function deleteAction()
    {
        $id = $this->_getParam('id');
        $form = Application_Model_Factory::createForm($this->_getParam('form'), (int) $id);

        if (!$form) {
            $this->_redirect('/admin/');
        }
        //TODO при удалении контента с содержанием файла, удалять и сам файл
        $form->deleteElement();
        $this->_redirect('/admin/' . $form->getType());
    }

    public function infoAction()
    {
        Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
        $this->_helper->layout->disableLayout();
        phpinfo();
    }

    public function testmailAction()
    {
        Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
        $this->_helper->layout->disableLayout();
        $name  = 'name';
        $phone = 'phone';
        ini_set('sendmail_from', 'kuzubtv@truds.ru');
        var_dump(ini_get('sendmail_from'));
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=utf-8\r\n";
        $headers .= "From: Server <kuzubtv@truds.ru>\r\n";
        $subject = 'Заяка на получение скидки на аренду оборудования';
        $msg = 'Имя: ' . $name . '. Телефон: '  . $phone;
        var_dump($subject);
        var_dump($msg);
        var_dump($headers);
        $res = mail('kubanecjony@list.ru', $subject, $msg, $headers);
        var_dump($res);
    }

    public function savesortAction()
    {
        Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
        $this->_helper->layout->disableLayout();

        $serialize = $this->_getParam('rent-list');

        if (!$serialize) {
            return false;
        }

        Application_Model_Factory::saveSortDataDigest($serialize, new Application_Model_DbTable_Rent(), 'rent');
        return true;
    }

    public function mailAction()
    {
        $this->view->mails = Application_Model_Mapper::getMailsStock();
    }
}